package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FarmMoneyVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.MyFarmMoneyListHolder;
import com.example.zhouwei.library.CustomPopWindow;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/6/17 13:46
 * 修改备注：
 * ================================================
 */
public class MyFarmMoneyActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.my_farm_money_tv_number)
    TextView myFarmMoneyTvNumber;
    @Bind(R.id.my_farm_money_layout_rule)
    LinearLayout myFarmMoneyLayoutRule;
    @Bind(R.id.my_farm_money_layout_mall)
    LinearLayout myFarmMoneyLayoutMall;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.my_farm_money_layout_type)
    RelativeLayout myFarmMoneyLayoutType;
    @Bind(R.id.my_farm_money_tv_type)
    TextView myFarmMoneyTvType;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String pointStatus = ""; //  0 获取 1支出 不传为全部
    private List<FarmMoneyVO> farmMoneyVOList = new ArrayList<>();

    private CustomPopWindow mCustomPopWindow;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_farm_money);
        setTitle("我的农币");
        setRightText("农币规则");
    }

    @Override
    public void initMember() {

        initPopWindow();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 15, 15));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyFarmMoneyListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_COIN_POINT_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_COIN_POINT_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        farmMoneyVOList = FastJsonUtil.getListBean(object.toString(), "body", "agriCoinRewardPointList", FarmMoneyVO.class);
        String  num = FastJsonUtil.getString(object.toString(),"pointTotalValue");
        myFarmMoneyTvNumber.setText(num);

        if (farmMoneyVOList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (farmMoneyVOList != null) {
            if (farmMoneyVOList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(farmMoneyVOList);
    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoActivity(MyFarmMoneyActivity.this, FarmMoneyRuleActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.my_farm_money_layout_rule, R.id.my_farm_money_layout_mall, R.id.my_farm_money_layout_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_farm_money_layout_rule:
                //GotoUtil.gotoActivity(MyFarmMoneyActivity.this, FarmMoneyRuleActivity.class);
                GotoUtil.gotoActivity(MyFarmMoneyActivity.this, CoinPresentListActivity.class);
                break;
            case R.id.my_farm_money_layout_mall:
                GotoUtil.gotoActivity(MyFarmMoneyActivity.this, MyExchangeListActivity.class);
                break;
            case R.id.my_farm_money_layout_type: // 切换积分展示类型
                showPopWindow();
                break;
        }
    }

    /**
     * 初始化popwindow
     */
    private void initPopWindow() {

        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_window_my_nb_type, null);
        TextView tvAll = (TextView) contentView.findViewById(R.id.pop_my_nb_tv_all);
        TextView tvGet = (TextView) contentView.findViewById(R.id.pop_my_nb_tv_get);
        TextView tvPay = (TextView) contentView.findViewById(R.id.pop_my_nb_tv_pay);

        tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pointStatus = "";
                page = 1;
                BusinessAccount.getCoinPointList(MyFarmMoneyActivity.this, String.valueOf(page), String.valueOf(limit), pointStatus, mHandler);
                myFarmMoneyTvType.setText("全部");
                mCustomPopWindow.dissmiss();
            }
        });
        tvGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pointStatus = "0";
                page = 1;
                BusinessAccount.getCoinPointList(MyFarmMoneyActivity.this, String.valueOf(page), String.valueOf(limit), pointStatus, mHandler);
                myFarmMoneyTvType.setText("收入");
                mCustomPopWindow.dissmiss();
            }
        });
        tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pointStatus = "1";
                page = 1;
                BusinessAccount.getCoinPointList(MyFarmMoneyActivity.this, String.valueOf(page), String.valueOf(limit), pointStatus, mHandler);
                myFarmMoneyTvType.setText("支出");
                mCustomPopWindow.dissmiss();
            }
        });

        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)//显示的布局，还可以通过设置一个View
                .size(WindowManager.LayoutParams.MATCH_PARENT, Utils.dip2px(this, 60))
                .setFocusable(true)//是否获取焦点，默认为ture
                .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams params = getWindow().getAttributes();
                        params.alpha = 1f;
                        getWindow().setAttributes(params);
                    }
                })
                .create();//创建PopupWindow
    }

    /**
     * 显示pop_window
     */
    private void showPopWindow() {
        mCustomPopWindow.showAsDropDown(myFarmMoneyLayoutType, 0, 0);//显示PopupWindow
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.7f;
        getWindow().setAttributes(params);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAccount.getCoinPointList(this, String.valueOf(page), String.valueOf(limit), pointStatus, mHandler);
    }

    @Override
    public void onLoadMore() {
        page++;
        BusinessAccount.getCoinPointList(this, String.valueOf(page), String.valueOf(limit), pointStatus, mHandler);
    }


}
